package com.sytc.camera.improttample;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Drag2Drop extends Application {

    @Override
    public void start(Stage primaryStage) {
        //拖放至少有两个区域，一个是源区域，一个是目标区域
        TabPane sourceTabPane = new TabPane();
        TabPane targetTabPane = new TabPane();

        BorderPane root = new BorderPane();
        root.setLeft(sourceTabPane);
        root.setRight(targetTabPane);

        Scene scene = new Scene(root, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();

        // 在源TabPane中添加一些初始的Tab
        Tab tab1 = new Tab("tabName1");
        Tab tab2 = new Tab("tabName2");
        Tab tab3 = new Tab("tabName3");
        // 注意，在实践中，无id时拖放无法触发事件，需要有id有数据传送才会生效拖放
        tab1.setId("tabId1");
        tab2.setId("tabId2");
        tab3.setId("tabId3");
        sourceTabPane.getTabs().addAll(tab1, tab2);
        targetTabPane.getTabs().addAll(tab3);

        // 设置源sourceTabPane的拖放触发处理
        sourceTabPane.setOnDragDetected(event -> {
            System.out.println("sourceTabPane.setOnDragDetected===");
            // onDragDetected(sourceTabPane);
            Tab selectedTab = sourceTabPane.getSelectionModel().getSelectedItem();
            if (selectedTab != null) {
                //将要拖放的tab的tabId放置到Dragboard中，供目标tabPane获取
                Dragboard dragboard = sourceTabPane.startDragAndDrop(TransferMode.MOVE);
                ClipboardContent content = new ClipboardContent();
                content.putString(selectedTab.getId());
                dragboard.setContent(content);
                System.out.println("sourceTabPane.setOnDragDetected==="+selectedTab.getId());
            }
            event.consume();
        });
        sourceTabPane.setOnDragOver(event -> {
            System.out.println("sourceTabPane.setOnDragOver==");
            if (event.getGestureSource() instanceof TabPane) {
                event.acceptTransferModes(TransferMode.MOVE);
            }
            event.consume();
        });
        sourceTabPane.setOnDragExited(event -> {
            System.out.println("sourceTabPane.setOnDragExited==");
            event.consume();
        });
        sourceTabPane.setOnDragDropped(event -> {
            System.out.println("sourceTabPane.setOnDragDropped==");
            Dragboard dragboard = event.getDragboard();
            if (dragboard.hasString()) {
                String tabId = dragboard.getString();
                Tab dragTab = null;
                for (Tab tab : targetTabPane.getTabs()) {
                    if (tab.getId().equals(tabId)) {
                        dragTab = tab;
                    }
                }
                if (dragTab != null) {
                    targetTabPane.getTabs().remove(dragTab);
                    sourceTabPane.getTabs().add(dragTab);
                    event.setDropCompleted(true);
                }
            }
            event.consume();
        });
        sourceTabPane.setOnDragEntered(event -> {
            System.out.println("sourceTabPane.setOnDragEntered==");
            event.consume();
        });
        sourceTabPane.setOnDragDone(event -> {
            System.out.println("sourceTabPane.setOnDragDone==");
            event.consume();
        });

        // 设置targetTabPane的拖放事件处理
        targetTabPane.setOnDragOver(event -> {
            System.out.println("===targetTabPane.setOnDragOver");
            if (event.getGestureSource() instanceof TabPane) {
                //注意下面必须设置为MOVE，否则无法接收拖来的tab数据
                event.acceptTransferModes(TransferMode.MOVE);
            }
            event.consume();
        });
        targetTabPane.setOnDragDropped(event -> {
            System.out.println("===targetTabPane.setOnDragDropped");
            Dragboard dragboard = event.getDragboard();
            if (dragboard.hasString()) {
                String tabId = dragboard.getString();
                Tab dragTab = null;
                for (Tab tab : sourceTabPane.getTabs()) {
                    if (tab.getId().equals(tabId)) {
                        dragTab = tab;
                    }
                }
                if (dragTab != null) {
                    sourceTabPane.getTabs().remove(dragTab);
                    targetTabPane.getTabs().add(dragTab);
                    event.setDropCompleted(true);
                }
            }
            event.consume();
        });
        targetTabPane.setOnDragDetected(event -> {
            System.out.println("==targetTabPane.setOnDragDetected");
            onDragDetected(targetTabPane);
            event.consume();
        });
        targetTabPane.setOnDragExited(event -> {
            System.out.println("==targetTabPane.setOnDragExited");
            event.consume();
        });
        targetTabPane.setOnDragEntered(event -> {
            System.out.println("==targetTabPane.setOnDragEntered");
            event.consume();
        });
        targetTabPane.setOnDragDone(event -> {
            System.out.println("==targetTabPane.setOnDragDone");
            event.consume();
        });
    }

    public void onDragDetected(TabPane sourceTabPane) {
        Tab selectedTab = sourceTabPane.getSelectionModel().getSelectedItem();
        if (selectedTab != null) {
            //将要拖放的tab的tabId放置到Dragboard中，供目标tabPane获取
            Dragboard dragboard = sourceTabPane.startDragAndDrop(TransferMode.MOVE);
            ClipboardContent content = new ClipboardContent();
            content.putString(selectedTab.getId());
            dragboard.setContent(content);
            System.out.println("sourceTabPane.setOnDragDetected==="+selectedTab.getId());
        }
    }

}